package com.cw.jeeyt.service.impl.process;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.process.ActReModelBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.process.ActReModelDao;
import com.cw.jeeyt.dao.model.process.ActReModelDo;
import com.cw.jeeyt.service.converter.process.ActReModelConverter;
import com.cw.jeeyt.service.intf.process.ActReModelService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 业务实现类
 *
 * @author chenw
 * @date 2019-03-23 23:37:05
 */
@Service
public class ActReModelServiceImpl implements ActReModelService {

    private static final Logger logger = LoggerFactory.getLogger(ActReModelServiceImpl.class);
    @Autowired
    private ActReModelDao actReModelDao;//数据库操作接口

    /**
     * 查询所有
     */
    @Override
    public List<ActReModelBo> queryActReModelList() throws Exception {
        return ActReModelConverter.doList2BoList(actReModelDao.queryActReModelList());
    }

    /**
     * 分页查询
     *
     * @param actReModelBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ActReModelBo> queryByPage(ActReModelBo actReModelBo, Integer startNum, Integer pageSize,
        Order order) throws Exception {

        if (actReModelBo == null) {
            logger.error("为空");
            return null;
        }
        PageInfo<ActReModelDo> pageInfo = actReModelDao.queryByPage(ActReModelConverter.bo2Do(actReModelBo), startNum,
            pageSize, order);
        return new PageInfo<>(pageInfo, ActReModelConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询信息
     *
     * @return
     */
    @Override
    public ActReModelBo getActReModelById(String id_) throws Exception {

        if (StringUtils.isEmpty(id_)) {
            logger.error("--主键id_为空--");
            return null;
        }
        return ActReModelConverter.do2Bo(actReModelDao.getById(id_));
    }

    /**
     * 新增
     *
     * @param actReModelBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ActReModelBo actReModelBo) throws Exception {

        if (actReModelBo == null) {
            logger.error("为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActReModelDo actReModel = ActReModelConverter.bo2Do(actReModelBo);
        actReModel.setId_(SequenceTool.nextId());
        Integer result = actReModelDao.insert(actReModel);
        return result;
    }

    /**
     * 新增不为空的字段
     *
     * @param actReModelBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ActReModelBo actReModelBo) throws Exception {

        if (actReModelBo == null) {
            logger.error("为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActReModelDo actReModel = ActReModelConverter.bo2Do(actReModelBo);
        actReModel.setId_(SequenceTool.nextId());
        Integer result = actReModelDao.insertSelective(actReModel);
        return result;
    }

    /**
     * 更新
     *
     * @param actReModelBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ActReModelBo actReModelBo) throws Exception {

        if (actReModelBo == null) {
            logger.error("为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActReModelDo actReModel = ActReModelConverter.bo2Do(actReModelBo);
        actReModelDao.update(actReModel);
    }

    /**
     * 更新不为空的字段
     *
     * @param actReModelBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ActReModelBo actReModelBo) throws Exception {

        if (actReModelBo == null) {
            logger.error("为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActReModelDo actReModel = ActReModelConverter.bo2Do(actReModelBo);

        actReModelDao.updateSelective(actReModel);
    }

    /**
     * 删除
     *
     * @param actReModelBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ActReModelBo actReModelBo) throws Exception {

        if (actReModelBo == null) {
            logger.error("为空");
            return;
        }
        ActReModelDo actReModel = ActReModelConverter.bo2Do(actReModelBo);
        actReModelDao.delete(actReModel);
    }
}
